package us.fourdesign.zptjob.util;

import java.util.List;

import us.fourdesign.zptjob.Activities.R;
import us.fourdesign.zptjob.DataHolder.JobInfo;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class JobAdpter extends BaseAdapter {
	private List<JobInfo> jobs;
	private Context mContext;
	private Resources res;
	private int resourceId;

	public JobAdpter(Context c, int resourseId) {
		res = c.getResources();
		jobs = new PartTimeJob().getJobs(res);
		this.resourceId = resourseId;
		mContext = c;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return jobs.size();
	}

	@Override
	public JobInfo getItem(int location) {
		// TODO Auto-generated method stub
		return jobs.get(location);
	}

	@Override
	public long getItemId(int location) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(resourceId, null);
		}
		ImageView cateIconView = (ImageView) convertView.findViewById(R.id.categoryicon);
		TextView cateName = (TextView) convertView.findViewById(R.id.categoryname);
		TextView proName = (TextView) convertView.findViewById(R.id.providername);
		TextView benifit = (TextView) convertView.findViewById(R.id.benifit);
		TextView jobBio = (TextView) convertView.findViewById(R.id.jobbio);
		TextView allTime = (TextView) convertView.findViewById(R.id.alltime);

		JobInfo job = jobs.get(position);

		AsyncImageLoader.AsyncImageDownLoadHelper(job.getProvider().getIcon(),cateIconView);
		cateName.setText(job.getCategory().getName());
		proName.setText(job.getProvider().getName());
		benifit.setText("¥" + job.getBenifit());
		jobBio.setText(job.getBio());
		StringBuilder sb = new StringBuilder();
		sb.append(job.getBegin_at().getMonth()).append(res.getString(R.string.month))
				.append(job.getBegin_at().getDay()).append(res.getString(R.string.day))
				.append(res.getString(R.string.to)).append(job.getEnd_at().getMonth())
				.append(res.getString(R.string.month)).append(job.getEnd_at().getDay())
				.append(res.getString(R.string.day)); 

		allTime.setText(sb.toString());

		return convertView;
	}

}
